package com.socialuni.sdk.factory.user.base;

import com.socialuni.sdk.api.model.RO.user.base.SocialUserFollowDetailRO;
import com.socialuni.sdk.dao.FollowDao;
import com.socialuni.sdk.model.DO.user.SocialPhoneNumDO;
import com.socialuni.sdk.model.DO.user.UserDO;
import com.socialuni.sdk.model.DO.user.UserImgDO;
import com.socialuni.sdk.model.RO.SocialUserDetailRO;
import com.socialuni.sdk.model.RO.UserImgRO;
import com.socialuni.sdk.store.SocialUserPhoneStore;
import com.socialuni.sdk.utils.UserUtils;
import com.socialuni.sdk.utils.model.DO.UserImgDOUtils;
import com.socialuni.sdk.utils.model.RO.UserImgROUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class SocialUserDetailROFactory {
    public static SocialUserPhoneStore socialUserPhoneStore;
    static FollowDao followDao;

    @Resource
    public void setFollowDao(FollowDao followDao) {
        SocialUserDetailROFactory.followDao = followDao;
    }

    @Resource
    public void setSocialUserPhoneStore(SocialUserPhoneStore socialUserPhoneStore) {
        SocialUserDetailROFactory.socialUserPhoneStore = socialUserPhoneStore;
    }

    public static SocialUserDetailRO getMineUserDetail() {
        UserDO userDO = UserUtils.getMineUser();
        return SocialUserDetailROFactory.getMineUserDetail(userDO);
    }

    public static SocialUserDetailRO getMineUserDetail(UserDO userDO) {
        return SocialUserDetailROFactory.getUserDetailRO(userDO, userDO);
    }

    public static SocialUserDetailRO getUserDetailRO(UserDO userDO, UserDO mineUser) {
        //用户关注粉丝数
        SocialPhoneNumDO socialUserPhoneDO = socialUserPhoneStore.findByUserId(userDO.getId());
        return SocialUserDetailROFactory.getUserDetailRO(userDO, mineUser, socialUserPhoneDO);
    }

    public static SocialUserDetailRO getUserDetailRO(UserDO userDO, UserDO mineUser, SocialPhoneNumDO socialUserPhoneDO) {
        //user基础信息
        SocialUserFollowDetailRO socialUserDetailFollowRO = SocialFollowUserROFactory.newSocialFollowUserRO(userDO, mineUser);
        SocialUserDetailRO userDetailVO = new SocialUserDetailRO(socialUserDetailFollowRO);

        boolean isMine = userDetailVO.getIsMine();

        //user详情信息
        if (isMine) {
            //为自己返回生日，方便修改，和手机号
            userDetailVO.setBirthday(userDO.getBirthday());
            if (socialUserPhoneDO != null) {
                String realPhoneNum = socialUserPhoneDO.getPhoneNum();
                if (StringUtils.isNotEmpty(realPhoneNum)) {
                    realPhoneNum = realPhoneNum.substring(0, 3) + "*****" + realPhoneNum.substring(8);
                    userDetailVO.setPhoneNum(realPhoneNum);
                }
            }
        }

        //用户图片
        List<UserImgDO> imgDOS = UserImgDOUtils.getImgs(userDO.getId());
        List<UserImgRO> imgVOS = UserImgROUtil.userImgDOToVOS(imgDOS);
        userDetailVO.setImgs(imgVOS);

        return userDetailVO;
    }
}
